<%--
  Created by IntelliJ IDEA.
  User: 16690
  Date: 2020/9/17
  Time: 18:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<span id="text">执行前文本内容</span>
<button type="button" id="button">点击修改</button>
<script type="text/javascript">

    let val = document.querySelector("#button");
    val.addEventListener("click" ,function () {
        let xmlHttpRequest = new XMLHttpRequest();

        let url="${pageContext.request.contextPath}/ajaxServlet";
        xmlHttpRequest.open("post",url,"true");
        console.log(xmlHttpRequest.readyState);
        xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttpRequest.setRequestHeader("Accept","*/*");

        xmlHttpRequest.onreadystatechange=function(){
            console.log(xmlHttpRequest.readyState);
            console.log(xmlHttpRequest)
            if(xmlHttpRequest.status==200&&xmlHttpRequest.readyState==4){
                document.querySelector("#text").innerHTML = xmlHttpRequest.responseText;

                let x = JSON.parse(xmlHttpRequest.responseText);
                console.log(x);
                console.log(x.user.password);
            }
        }

        let user="username=张三&password=123123";
        xmlHttpRequest.send(user);
    })

</script>
</body>
</html>

